package com.zhz.demothymeleaf.web.controller;

import com.sun.deploy.net.HttpResponse;
import com.zhz.demothymeleaf.entity.User;
import com.zhz.demothymeleaf.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class LoginController {
    @Resource
    private UserService userService;

    @GetMapping({"/login","/"})
    public String login(){
        return "login";
    }

    @PostMapping("/doLogin")
    public String doLogin(String username, String password, HttpSession session) {
        User user = userService.login(username,password);
        if(user!=null){
            session.setAttribute("loginUser",user);
            return "redirect:/user/main";
        }
        return "login";
    }

    @GetMapping("/logOut")
    public String logOut(HttpSession session){
        session.removeAttribute("loginUser");
        return "redirect:/login";
    }
}
